package unit_test;

import static org.junit.Assert.*;
import hirondelle.date4j.DateTime;

import org.junit.Test;

import common.Task;
import common.TaskType;

/**
 * Get task duration tester
 */

//@author A0088498A
public class TaskDurationTest {
	@Test
	public void nullTest() {
		DateTime startingTime = null;
		DateTime endingTime = null;
		Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

		long durationInMinutes = task.getDuration();

		assertEquals(durationInMinutes, -1);
	}

	@Test
	public void identicalTest() {
		try {
			DateTime startingTime = new DateTime("2010-01-19 23:59:59");
			DateTime endingTime = new DateTime("2010-01-19 23:59:59");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, 0);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void noDateTest() {
		try {
			DateTime startingTime = new DateTime("2010-01-18"); // should be
																// treat as
																// "2010-01-18 00:00:00"
			DateTime endingTime = new DateTime("2010-01-19 23:59:59");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, 2879); // 2 days minus 1 minute
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void evertTest() {
		try {
			// Starting time is after ending time
			DateTime startingTime = new DateTime("2010-01-19");
			DateTime endingTime = new DateTime("2010-01-18");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, -1);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void oneNullTest() {
		try {
			// One task is null
			DateTime startingTime = null;
			DateTime endingTime = new DateTime("2010-01-18");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, -1);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void smallTest() {
		try {
			// 5 minutes different, seconds should be ignore
			DateTime startingTime = new DateTime("2010-01-18 12:50:12");
			DateTime endingTime = new DateTime("2010-01-18 12:55:59");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, 5);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void largeTest() {
		try {
			// months different
			DateTime startingTime = new DateTime("2010-01-18 12:50:12");
			DateTime endingTime = new DateTime("2018-05-21 12:55:59");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, 4384805);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void extremeTest() {
		try {
			// A hundred years, should overflow the number
			DateTime startingTime = new DateTime("2010-01-18 12:50:12");
			DateTime endingTime = new DateTime("2110-01-18 12:50:12");
			Task task = new Task("", TaskType.timed, "", startingTime, endingTime);

			long durationInMinutes = task.getDuration();

			assertEquals(durationInMinutes, -1);
		} catch (Exception e) {
			e.printStackTrace();
			fail();
		}
	}
}
